#include "artVcontact.hpp"
#include "SEEKFREE_IPS200_PARALLEL8.h"
namespace artVcontact
{

unsigned char findblobs_sem_releas_flag=0;


uint8 ARTV_rx_buffer;
lpuart_transfer_t   ARTV_receivexfer;
lpuart_handle_t     ARTV_g_lpuartHandle;

unsigned char ARTV_DataToSend[100];
unsigned char ARTV_DataGet[60];

long int sorted_pictures[20];
uint8_t sorted_num=0;


uint8_t labels_to_big_order(long int sort)
{
	uint8_t b_sort=0;

	if(     sort==4||sort==5||sort==6||sort==9||sort==11)
	b_sort=1;
	else if(sort==0||sort==1||sort==7||sort==8||sort==10)
	b_sort=2;
	else if(sort==2||sort==3||sort==12||sort==13||sort==14)
	b_sort=3;

	return b_sort;
}
uint8_t labels_to_little_order(long int sort)
{
	uint8_t l_sort=0;

	if(     sort==6||sort==10||sort==14)
	l_sort=1;
	else if(sort==0||sort==9||sort==13)
	l_sort=2;
	else if(sort==4||sort==7||sort==12)
	l_sort=3;
	else if(sort==3||sort==5||sort==8)
	l_sort=4;
	else if(sort==1||sort==2||sort==11)
	l_sort=5;

	return l_sort;
}

void artv_uart_callback(LPUART_Type *base, lpuart_handle_t *handle, status_t status, void *userData)
{
    if(kStatus_LPUART_RxIdle == status)
    {
        ARTV_GetByte( ARTV_rx_buffer);
			  //uart_putchar(USART_2,ART_rx_buffer);
    }
    
    handle->rxDataSize = ARTV_receivexfer.dataSize;  
    handle->rxData = ARTV_receivexfer.data;          
}

void ARTV_UART_Init(void)
{
	uart_init(USART_4, 115200,UART4_TX_C16,UART4_RX_C17);
	//NVIC_SetPriority(USART_1,15);         
	uart_rx_irq(USART_4,1);
	
	ARTV_receivexfer.dataSize = 1;
	ARTV_receivexfer.data = &ARTV_rx_buffer;
	
	uart_set_handle(USART_4, &ARTV_g_lpuartHandle, artv_uart_callback, NULL, 0, ARTV_receivexfer.data, 1);
}


void ARTV_SendPar(unsigned short int _id,long int _val)
{
	  unsigned char _cnt = 0;
	
    ARTV_DataToSend[_cnt++] = 0xAA;
    ARTV_DataToSend[_cnt++] = 0xFF;
    ARTV_DataToSend[_cnt++] = 0xE2;
    ARTV_DataToSend[_cnt++] = 4;

	  ARTV_DataToSend[_cnt++] = BYTE0(_id);
    ARTV_DataToSend[_cnt++] = BYTE1(_id);
    ARTV_DataToSend[_cnt++] = BYTE0(_val);
    ARTV_DataToSend[_cnt++] = BYTE1(_val);
    ARTV_DataToSend[_cnt++] = BYTE2(_val);
    ARTV_DataToSend[_cnt++] = BYTE3(_val);

	  ARTV_DataToSend[3]=_cnt-4;
    unsigned char sc = 0;
    unsigned char ac = 0;

    for (unsigned char i = 0; i < (ARTV_DataToSend[3] + 4); i++) {
        sc += ARTV_DataToSend[i];
        ac += sc;
    }
    ARTV_DataToSend[_cnt++] = sc;
    ARTV_DataToSend[_cnt++] = ac;
    for (unsigned char temp = 0; temp < _cnt; temp++) {
        uart_putchar(USART_4,ARTV_DataToSend[temp]);
    }
}

void ARTV_SendCheck(unsigned char _id,unsigned char _sc,unsigned char _ac)
{
	  unsigned char _cnt = 0;
	
    ARTV_DataToSend[_cnt++] = 0xAA;
    ARTV_DataToSend[_cnt++] = 0xFF;
    ARTV_DataToSend[_cnt++] = 0x00;
    ARTV_DataToSend[_cnt++] = 3;

    ARTV_DataToSend[_cnt++] = _id;
    ARTV_DataToSend[_cnt++] = _sc;
    ARTV_DataToSend[_cnt++] = _ac;



	
    unsigned char sc = 0;
    unsigned char ac = 0;

    for (unsigned char i = 0; i < (ARTV_DataToSend[3] + 4); i++) {
        sc += ARTV_DataToSend[i];
        ac += sc;
    }
    ARTV_DataToSend[_cnt++] = sc;
    ARTV_DataToSend[_cnt++] = ac;
    for (unsigned char temp = 0; temp < _cnt; temp++) {
        uart_putchar(USART_4,ARTV_DataToSend[temp]);
    }
}

void ARTV_Anl(void)
{
	unsigned char sc=0;
	unsigned char ac=0;
	
	unsigned char _datalen = ARTV_DataGet[3];
	for(unsigned i=0;i<ARTV_DataGet[3]+4;i++)
	{
		sc+=ARTV_DataGet[i];
		ac+=sc;
	}
	if(sc!=ARTV_DataGet[ARTV_DataGet[3]+4]||ac!=ARTV_DataGet[ARTV_DataGet[3]+5])
		return;
	
	if(ARTV_DataGet[2]==0xE1)
	{
		unsigned short int _id =ARTV_DataGet[4]+(unsigned short int)(ARTV_DataGet[5]<<8);
		switch(_id)
		{
			case 1:
				// ARTV_SendPar(_id,Car_DistanceX);break;
			case 2:
				// ARTV_SendPar(_id,0);break;
			case 3:
				// ARTV_SendPar(_id,0);break;
			case 10:
				// ARTV_SendPar(_id,ARTV_Partest);break;
			default:
				ARTV_SendPar(_id,0);break;			
		}
		//ANODT_SendPar(_id,userPartest);
	}
	
	
	else if(ARTV_DataGet[2]==0xE2)
	{
		unsigned short int _id =ARTV_DataGet[4]+(unsigned short int)(ARTV_DataGet[5]<<8);
		switch(_id)
		{
			case 0:
			{
				rt_sem_release(find_rect_ok_sem);
				break;
			}

			case 1:
			{
				sorted_pictures[sorted_num]=*(long int*)(&ARTV_DataGet[6]);
				rt_sem_release(recognition_ok_sem);
				sorted_num++;
				break;
			}
			case 2:
			{
				if(666==*(long int*)(&ARTV_DataGet[6])&&findblobs_sem_releas_flag==1)
				{
					findblobs_sem_releas_flag=0;
					rt_sem_release(findblobs_ok_sem);
				}
				break;
			}

			default:
				ARTV_SendPar(_id,0);break;
		}
		ARTV_SendCheck(ARTV_DataGet[2],ARTV_DataGet[ARTV_DataGet[3]+4],ARTV_DataGet[ARTV_DataGet[3]+5]);
	}
	
}


void ARTV_GetByte(unsigned char data)
{
	static unsigned char _sta = 0;
	static unsigned char _datalen = 0;
	static unsigned char _datacnt = 0;
	if(_sta ==0)
	{
	    ARTV_DataGet[0] = data;
		_datacnt=0;
		if(data == 0xAA)
		   _sta = 1;
  }
	else if(_sta == 1)
	{
		ARTV_DataGet[1] = data;
		_sta = 2;
	}
  else if(_sta == 2)
	{
	    ARTV_DataGet[ 2] = data;
		_sta = 3;
	}
	else if(_sta == 3)
	{
		if(data > 50)
		   _sta = 0;
		else
		{
		_sta = 4;
		ARTV_DataGet[3]= data;
		_datalen = data;
		}
  }
	else if(_sta ==4)
	{
		ARTV_DataGet[4+_datacnt++]= data;
		if(_datacnt >= _datalen)
	     _sta = 5;
	}
	else if(_sta==5)
	{
		ARTV_DataGet[4+_datacnt++]=data;
		_sta= 6;
	}
	else if(_sta==6)
	{
		ARTV_DataGet[4+_datacnt++]=data;
		_sta=0;
		ARTV_Anl();
		
	}
}

}//namespace artVcontact